
UniqueID = nil;

local function setUniqueID( msg )

	UniqueID = msg:ReadShort();
	
end
usermessage.Hook( "setUniqueID", setUniqueID );

CharacterSaves = { }

function addCharacter( msg )

	local name = msg:ReadString();
	local model = msg:ReadString();
	local id = msg:ReadShort();
	
	table.insert( CharacterSaves, { Name = name, Model = model, ID = id } );

end
usermessage.Hook( "addCharacter", addCharacter );

--You can have multiple id cards
IDCards = { }

CreatePlayerMenu();

function GiveIDCard( msg )

	local id = msg:ReadShort();
	local name = msg:ReadString();
	local model = msg:ReadString();
	local age = msg:ReadShort();
	local cid = msg:ReadShort();
	local cm = msg:ReadString();
	
	for k, v in pairs( IDCards ) do
	
		if( v.ID == id ) then
			return;
		end
	
	end
	
	--table.insert( IDCards, { ID = id, Name = name, Model = model, Age = age, CID = cid, ClassMark = cm } );
	local function CreateGivenIDCardFrame()

		PlayerGivenIDCard = CreateBPanel( name .. "'s ID Card", 10, 10, 300, 200 );
		
		PlayerGivenIDCard:CanClose( true );
		
		PlayerGivenIDCard.OnMousePressed = function( self )
		
			self.Dragging = true;
			self.DragXOffset, self.DragYOffset = gui.MousePos();
			
			local x, y = self:GetPos();
			
			self.DragXOffset = self.DragXOffset - x;
			self.DragYOffset = self.DragYOffset - y;
			
		end
		
		PlayerGivenIDCard.OnMouseReleased = function( self )
		
			self.Dragging = false;
		
		end
		
		PlayerGivenIDCard.Think = function( self )

			if( not input.IsMouseDown( MOUSE_LEFT ) ) then
			
				self.Dragging = false;
			
			end
		
			if( self.Dragging ) then
		
				local mx, my = gui.MousePos();
				
				mx = mx - self.DragXOffset;
				my = my - self.DragYOffset;
			
				self:SetPos( mx, my );

			end
		
		end
		
		PlayerGivenIDCard.IDImage = vgui.Create( "DModelPanel", PlayerGivenIDCard );

		PlayerGivenIDCard.IDImage:SetPos( 15, 50 );
		PlayerGivenIDCard.IDImage:SetSize( 84, 84 );
		PlayerGivenIDCard.IDImage:SetFOV( 30 );
		
		PlayerGivenIDCard.IDImage:SetLookAt( Vector( 0, 0, 62 ) );
		PlayerGivenIDCard.IDImage:SetCamPos( Vector(45, -5, 55 ) );
		
		PlayerGivenIDCard.IDImage.LayoutEntity = function()
			
		end
		
		PlayerGivenIDCard.IDImage.Think = function()
		
			if( PlayerGivenIDCard.IDImage.Model ~= LocalPlayer():GetModel() ) then
			
				PlayerGivenIDCard.IDImage.Model = model;
				PlayerGivenIDCard.IDImage:SetModel( model );
			
			end
		
		end
		
		PlayerGivenIDCard.Paint = function( self )
		
			draw.RoundedBox( 8, 0, 0, self:GetWide(), self:GetTall(), Color( 252, 251, 225, 255 ) );
			draw.RoundedBox( 8, 4, 4, self:GetWide() - 8, self:GetTall() - 8, Color( 236, 236, 221, 255 ) );
				
			local x, y = PlayerGivenIDCard.IDImage:GetPos();
			draw.RoundedBox( 0, x, y, PlayerGivenIDCard.IDImage:GetWide(), PlayerGivenIDCard.IDImage:GetTall(), Color( 200, 200, 200, 255 ) );
	   
			draw.DrawText( "Universal Union Acquisition X17", "IDHeader", self:GetWide() / 2, 3, Color( 0, 0, 0, 255 ), 1 );
			draw.DrawText( "Proof of Naturalised Citizenship", "IDHeader2", self:GetWide() / 2, 25, Color( 0, 0, 0, 255 ), 1 );
			
			local gender = "Male";
			
			if( string.find( string.lower( model ) , "female" ) ) then
			
				gender = "Female";
			
			end
			
			draw.DrawText( name, "IDInfo", self:GetWide() - 5, 55, Color( 0, 0, 0, 255 ), 2 );
			draw.DrawText( "Permissions:", "IDInfo", x + PlayerGivenIDCard.IDImage:GetWide() / 2, y + PlayerGivenIDCard.IDImage:GetTall(), Color( 0, 0, 0, 255 ), 1 );
			draw.DrawText( gender, "IDInfo", self:GetWide() - 5, 71, Color( 0, 0, 0, 255 ), 2 );
			draw.DrawText( "Age: " .. age, "IDInfo", self:GetWide() - 5, 86, Color( 0, 0, 0, 255 ), 2 );
			draw.DrawText( "C-Code: " .. cid, "IDInfo", self:GetWide() - 5, 102, Color( 0, 0, 0, 255 ), 2 );
			draw.DrawText( "STATUS: Naturalised\nCLASS: " .. cm .. "\nRESTRICTIONS: ***", "IDInfo2", 8, 148, Color( 0, 0, 0, 255 ) );
			draw.DrawText( "If you are in possession of a Citizenship\nIdentification Card, you are subject to the\nUniversal Union codes of conduct, including\npossible cross-examination of citizenship\ndetails by the local Civil Authority enforcement\n teams. This includes recession of therights and\nfreedoms previously documented in classified or\nunauthorised Acquisition X17 texts.", "IDInfo3", self:GetWide() - 3, 120, Color( 0, 0, 0, 255 ), 2 );
			
			surface.SetMaterial( Material( "stampy" ) );
			surface.SetDrawColor( 255, 255, 255, 80 );
			surface.DrawTexturedRect( self:GetWide() - 70, self:GetTall() - 70, 64, 64 );
		end
		PlayerGivenIDCard:MakePopup();
		PlayerGivenIDCard:CanClose( true );
	end
	
	CreateGivenIDCardFrame()
	
end
usermessage.Hook( "GIDC", GiveIDCard );


if( AccountMenu ) then

	if( AccountMenu.TitleBar ) then

		AccountMenu.TitleBar:Remove();
		AccountMenu.TitleBar = nil;
		
	end
		
	AccountMenu:Remove();
	AccountMenu = nil;

end

function CreateAddDoorOwnerMenu( msg )
	
	AddDoorOwnerMenu = CreateBPanel( "Give out a spare set of keys.", ( ScrW() * .5 ) - 100, ( ScrH() * .5 ) - 25, 200, 50 );
	AddDoorOwnerMenu:CanClose( true );
	AddDoorOwnerMenu:CanDrag( false );
	AddDoorOwnerMenu:SetBodyColor( Color( 40, 40, 40, 220 ) );
	
	AddDoorOwnerMenu:AddLabel( "Recipitant:", "NewChatFont", 13, 8, Color( 255, 255, 255, 255 ) );
	
	local x, y = AddDoorOwnerMenu:GetPos();


	AddDoorOwnerMenu.NameEntry = vgui.Create( "DTextEntry", AddDoorOwnerMenu );
	AddDoorOwnerMenu.NameEntry:SetPos( x + 80, y + 8 );
	AddDoorOwnerMenu.NameEntry:SetSize( 100, 15 );
	AddDoorOwnerMenu.NameEntry:MakePopup();
	
	ApplyMaxCharacters( AddDoorOwnerMenu.NameEntry, 20 );
	
	AddDoorOwnerMenu.SetOwner = AddDoorOwnerMenu:AddButton( "Give Keys", 60, 25);
	
	AddDoorOwnerMenu.SetOwner.Action = function()
		
		RunConsoleCommand( "eng_ado", AddDoorOwnerMenu.NameEntry:GetValue() );
	
	end
	
	gui.EnableScreenClicker( true );

end
usermessage.Hook( "PADO", CreateAddDoorOwnerMenu );

function RemoveAddDoorOwnerMenu( msg )
	
	if( AddDoorOwnerMenu and AddDoorOwnerMenu.TitleBar ) then
	
		AddDoorOwnerMenu.TitleBar:Remove();
		AddDoorOwnerMenu:Remove();

	end

end
usermessage.Hook( "RADO", RemoveAddDoorOwnerMenu );

function CreateRadioMenu( msg )
	
	RadioMenu = CreateBPanel( "Set your radio frequency", ( ScrW() * .5 ) - 100, ( ScrH() * .5 ) - 25, 200, 50 );
	RadioMenu:CanClose( true );
	RadioMenu:CanDrag( false );
	RadioMenu:SetBodyColor( Color( 40, 40, 40, 220 ) );
	
	RadioMenu:AddLabel( "Frequency:", "NewChatFont", 13, 8, Color( 255, 255, 255, 255 ) );
	
	local x, y = RadioMenu:GetPos();


	RadioMenu.FreqEntry = vgui.Create( "DTextEntry", RadioMenu );
	RadioMenu.FreqEntry:SetPos( x + 80, y + 8 );
	RadioMenu.FreqEntry:SetSize( 100, 15 );
	RadioMenu.FreqEntry:MakePopup();
	
	ApplyMaxCharacters( RadioMenu.FreqEntry, 20 );
	
	RadioMenu.SetFreq = RadioMenu:AddButton( "Set Frequency", 60, 25);
	
	RadioMenu.SetFreq.Action = function()
	
		if( not tonumber( RadioMenu.FreqEntry:GetValue() ) ) then
		
			CreateOkPanel( "Invalid Frequency", "Invalid Number." );
			return;
		
		end
	
		if( tonumber( RadioMenu.FreqEntry:GetValue() ) < 0 or
			tonumber( RadioMenu.FreqEntry:GetValue() ) > 999.9 ) then
		
			CreateOkPanel( "Invalid Frequency", "Frenquency must be between 0 and 999.9" );
			return;		
		
		end
		
		RunConsoleCommand( "eng_changefrequency", RadioMenu.FreqEntry:GetValue() );
	
	end
	
	gui.EnableScreenClicker( true );

end
usermessage.Hook( "PRM", CreateRadioMenu );

function RemoveRadioMenu( msg )
	
	if( RadioMenu and RadioMenu.TitleBar ) then
	
		RadioMenu.TitleBar:Remove();
		RadioMenu:Remove();

	end

end
usermessage.Hook( "RRM", RemoveRadioMenu );

function NoRadio( msg )
	
	CreateOkPanel( "Error", "You need a radio to do this." );
	gui.EnableScreenClicker( true );

end
usermessage.Hook( "NR", NoRadio );


function LoanMenu()

	LoanMenu = CreateBPanel( "Pay/Take a loan", ( ScrW() * .5 ) - 125, ( ScrH() * .5 ) - 40, 250, 80 );
	LoanMenu:CanClose( true );
	LoanMenu:CanDrag( false );
	LoanMenu:SetBodyColor( Color( 40, 40, 40, 220 ) );
	
	LoanMenu:AddLabel( "Take Loan:", "NewChatFont", 5, 8, Color( 255, 255, 255, 255 ) );
	LoanMenu:AddLabel( "Pay Loan:", "NewChatFont", 5, 25, Color( 255, 255, 255, 255 ) );
	
	local x, y = LoanMenu:GetPos();

	LoanMenu.LoanTakeEntry = vgui.Create( "DTextEntry", LoanMenu );
	LoanMenu.LoanTakeEntry:SetPos( x + 80, y + 8 );
	LoanMenu.LoanTakeEntry:SetSize( 163, 15 );
	LoanMenu.LoanTakeEntry:MakePopup();
	
	LoanMenu.PayLoanEntry = vgui.Create( "DTextEntry", LoanMenu );
	LoanMenu.PayLoanEntry:SetPos( x + 103, y + 25 );
	LoanMenu.PayLoanEntry:SetSize( 140, 15 );
	LoanMenu.PayLoanEntry:MakePopup();
	
	LoanMenu.TakeLoanButton = LoanMenu:AddButton( "Take Loan", 5, 50 );
	LoanMenu.PayLoan = LoanMenu:AddButton( "Pay Loan", 75, 50 );
	LoanMenu.GetLoan = LoanMenu:AddButton( "Loan Amount", 140, 50 );
	
	LoanMenu.TakeLoanButton.Action = function()
	
		RunConsoleCommand( "eng_takeloan", LoanMenu.LoanTakeEntry:GetValue() )
		
	end

	LoanMenu.PayLoan.Action = function()

		RunConsoleCommand( "eng_payloan", LoanMenu.PayLoanEntry:GetValue() )
		
	end	
	
	LoanMenu.GetLoan.Action = function()
	
		RunConsoleCommand( "eng_loanamount" )
		
	end	
		
	
end	
usermessage.Hook( "OLM", LoanMenu ) 

function CreateNameMenu( msg )

	local propertyname = msg:ReadString();
	local doorname = msg:ReadString();
	
	NameMenu = CreateBPanel( "Set this door/property name", ( ScrW() * .5 ) - 125, ( ScrH() * .5 ) - 40, 250, 80 );
	NameMenu:CanClose( true );
	NameMenu:CanDrag( false );
	NameMenu:SetBodyColor( Color( 40, 40, 40, 220 ) );
	
	NameMenu:AddLabel( "Door Name:", "NewChatFont", 5, 8, Color( 255, 255, 255, 255 ) );
	NameMenu:AddLabel( "Property Name:", "NewChatFont", 5, 25, Color( 255, 255, 255, 255 ) );
	
	local x, y = NameMenu:GetPos();

	NameMenu.DoorNameEntry = vgui.Create( "DTextEntry", NameMenu );
	NameMenu.DoorNameEntry:SetPos( x + 80, y + 8 );
	NameMenu.DoorNameEntry:SetSize( 163, 15 );
	NameMenu.DoorNameEntry:SetText( doorname );
	NameMenu.DoorNameEntry:MakePopup();
	
	ApplyMaxCharacters( NameMenu.DoorNameEntry, 20 );
	
	NameMenu.PropNameEntry = vgui.Create( "DTextEntry", NameMenu );
	NameMenu.PropNameEntry:SetPos( x + 103, y + 25 );
	NameMenu.PropNameEntry:SetSize( 140, 15 );
	NameMenu.PropNameEntry:SetText( propertyname );
	NameMenu.PropNameEntry:MakePopup();
	
	
	ApplyMaxCharacters( NameMenu.PropNameEntry, 20 );
	
	NameMenu.SetNames = NameMenu:AddButton( "Name Property", 75, 50);
	
	NameMenu.SetNames.Action = function()
	
		if( string.gsub( NameMenu.DoorNameEntry:GetValue(), " ", "" ) == "" ) then
		
			CreateOkPanel( "Invalid door name.", "Door name cannot be blank" );
			return;		
		
		end
		
		--[[if( string.gsub( NameMenu.PropNameEntry:GetValue(), " ", "" ) == "" ) then
		
			CreateOkPanel( "Invalid property name.", "Property name is blank, queer." );
			return;		
		
		end]]--
		
		RunConsoleCommand( "eng_setdoorname", NameMenu.DoorNameEntry:GetValue() );
		RunConsoleCommand( "eng_setpropertyname", NameMenu.PropNameEntry:GetValue() );
	
	end
	
	gui.EnableScreenClicker( true );

end
usermessage.Hook( "PNM", CreateNameMenu );

function RemoveNameMenu( msg )
	
	if( NameMenu and NameMenu.TitleBar ) then
	
		NameMenu.TitleBar:Remove();
		NameMenu:Remove();

	end

end
usermessage.Hook( "RNM", RemoveNameMenu );


function EndFancyGayIntro()

	TS.FancyGayIntro = false;
	TS.MapView = false;

	HideMouse();

end
usermessage.Hook( "EFGI", EndFancyGayIntro );

function CivilProtectionUmsg( msg )

	local name = msg:ReadString()

	table.insert( OverWatchLines, { Line = "<:: Vital Signs for " .. name .. " has been lost ::>", TimeDone = CurTime() + 15 } );
	
end
usermessage.Hook( "BSL", CivilProtectionUmsg )	

function MapViewOff()

	TS.MapView = false;

end
usermessage.Hook( "MVO", MapViewOff );

CPChangeModels = {

	"models/purvis/male_01_metrocop.mdl",
    "models/purvis/male_24_metrocop.mdl",
    "models/purvis/male_03_metrocop.mdl",
    "models/purvis/male_04_metrocop.mdl",
    "models/purvis/male_05_metrocop.mdl",
    "models/purvis/male_06_metrocop.mdl",
    "models/purvis/male_07_metrocop.mdl",
    "models/purvis/male_08_metrocop.mdl",
    "models/purvis/male_18_metrocop.mdl",
    "models/purvis/male_12_metrocop.mdl",
    "models/purvis/male_25_metrocop.mdl",
    "models/c08cop.mdl",
    "models/police_female_blondecp.mdl",
    "models/c08cop_female.mdl",
    "models/police_female_finalcop.mdl",
	"models/metrold.mdl",
				
}			

function StartCPModelChanger()

	ModelChoosePanel = CreateBPanel( "Model Choose Panel", ScrW() / 2 - 300, math.Clamp( ScrH() / 2 - 210, ScrH() * .1 + 70, 9999 ), 300, 347 )
	ModelChoosePanel:CanDrag( true )
	ModelChoosePanel:EnableScrolling( true )
	
	ModelPanels = { }
	
	local x = 0
	local y = 0
	
	for k, v in pairs( CPChangeModels ) do
	
		ModelPanels[v] = vgui.Create( "SpawnIcon", ModelChoosePanel )
		ModelPanels[v]:SetModel( v )
		ModelPanels[v]:SetPos( x + 5, y + 5 )
		
		x = x + 65
		
		if( x >= 226 ) then
			y = y + 65
			x = 0 
		end	
		
		ModelChoosePanel:AddObject( ModelPanels[v] )
		
		ModelPanels[v].DoClick = function()
		
			RunConsoleCommand( "rp_setcpmodel", v )
			
			ModelChoosePanel:Remove()
			ModelChoosePanel = nil
			
			gui.EnableScreenClicker( false )
			
		end	
		
	end	
	
	gui.EnableScreenClicker( true )
	
	
end	
usermessage.Hook( "MCP", StartCPModelChanger )
		
--Receive item data, cept for description
function RecItemData( msg )

	local name = msg:ReadString();
	local model = msg:ReadString();
	local lookat = msg:ReadVector();
	local campos = msg:ReadVector();
	local fov = msg:ReadShort();
	local w = msg:ReadShort();
	local h = msg:ReadShort();
	local flags = msg:ReadString();
	local price = msg:ReadShort();
	local contains = msg:ReadString();
	
	local id = string.sub( name, 1, string.find( name, ":" ) - 1 );
	name = string.sub( name, string.find( name, ":" ) + 1 );
	
	TS.ItemsData[id] = { Name = name, Model = model, Desc = "", LookAt = lookat, CamPos = campos, Fov = fov, Width = w, Height = h, Flags = flags, Price = price, ContainsModel = contains };

end
usermessage.Hook( "RID", RecItemData );

function RecDescPiece( msg )

	local id = msg:ReadString();
	local desc = msg:ReadString();
	
	TS.ItemsData[id].Desc = TS.ItemsData[id].Desc .. desc;

end
usermessage.Hook( "RDP", RecDescPiece );

function RecItemHUDInfo( msg )

	local id = msg:ReadShort();
	local name = msg:ReadString();
	
	local ent = ents.GetByIndex( id );
	
	ent:GetTable().ItemName = name;

end
usermessage.Hook( "RIHI", RecItemHUDInfo );

LetterContent = "";

if( WritingVGUI ) then

	if( WritingVGUI.Menu ) then 
		WritingVGUI.Menu:Remove();
		WritingVGUI.Menu = nil;
	end	

	WritingVGUI:Remove();

end

WritingVGUI = nil;
PreviewLetterOn = false;
LetterCache = "";
UnfLetterContent = "";
LetterContent = "";

function LetterWritingPrompt( msg )

	local content = msg:ReadString();
	
	LetterCache = LetterCache .. content;
	
	WritingVGUI = vgui.Create( "Panel" );
	if( ScrH() > 600 ) then
		WritingVGUI:SetPos( ScrW() / 2 - 300, 100 );
	else
		WritingVGUI:SetPos( ScrW() / 2 - 300, 0 );
	end
	WritingVGUI:SetSize( 600, 600 );
	WritingVGUI.Paint = function()
	
		draw.RoundedBox( 0, 0, 0, WritingVGUI:GetWide(), WritingVGUI:GetTall(), Color( 255, 255, 255, 200 ) );
		
	end
	
	local x, y = WritingVGUI:GetPos();
	
	WritingVGUI.TextEntry = vgui.Create( "DTextEntry", WritingVGUI );
	WritingVGUI.TextEntry:SetPos( x + 5, y + 5 );
	WritingVGUI.TextEntry:SetSize( 590, 590 );
	WritingVGUI.TextEntry:SetText( WritingVGUI.TextEntry:GetValue() .. LetterCache );
	WritingVGUI.TextEntry:SetMultiline( true );
	WritingVGUI.TextEntry:MakePopup();

	local function Finish()
	
		WritingVGUI:SetVisible( false ); 
		WritingVGUI.TextEntry:SetVisible( false );
		WritingVGUI.Menu:SetVisible( false );
		PreviewLetterOn = false;
	
		local parts = math.ceil( string.len( WritingVGUI.TextEntry:GetValue() ) / 200 );

		RunConsoleCommand( "eng_sl", "" );
		 
		local delay = 0;
		 
		for n = 1, parts do
		
			timer.Simple( delay, RunConsoleCommand, "eng_slp", string.gsub( string.sub( WritingVGUI.TextEntry:GetValue(), ( n - 1 ) * 200 + 1, n * 200 ), "\n", "@n" ) );
			delay = delay + .3;
			
		end
		
		timer.Simple( delay, RunConsoleCommand, "eng_el", "" );
	
	end

	local _, y = WritingVGUI:GetPos();

	WritingVGUI.Menu = CreateBPanel( "Menu", ScrW() / 2 + 300, y, 100, 110 );
	WritingVGUI.Menu:CanSeeTitle( false );
	WritingVGUI.Menu:AddButton( "Edit", 5, 5, function() WritingVGUI:SetVisible( true ); WritingVGUI.TextEntry:SetVisible( true ); PreviewLetterOn = false; end );
	WritingVGUI.Menu:AddButton( "Preview", 5, 30, function() WritingVGUI:SetVisible( false ); WritingVGUI.TextEntry:SetVisible( false ); PreviewLetterOn = true; gui.EnableScreenClicker( true ); LetterContent = FormatLine( WritingVGUI.TextEntry:GetValue(), "LetterFont", 580 ); end );
	WritingVGUI.Menu:AddButton( "Close", 5, 55, function() WritingVGUI:SetVisible( false ); WritingVGUI.TextEntry:SetVisible( false ); WritingVGUI.Menu:SetVisible( false ); PreviewLetterOn = false; HideMouse(); end );
	WritingVGUI.Menu:AddButton( "Finish", 5, 80, Finish );
	
end
usermessage.Hook( "LWP", LetterWritingPrompt );

function StartLetter( msg )

	UnfLetterContent = "";
	LetterContent = "";

end
usermessage.Hook( "SL", StartLetter );

if( LetterVGUI ) then
	
	if( LetterVGUI.Menu ) then
		LetterVGUI.Menu:Remove();
	end

	LetterVGUI:Remove();
end
LetterVGUI = nil;
function DisplayLetter( msg )
	
	if( LetterVGUI ) then
		LetterVGUI:Remove();
	end
	
	local y = 0;

	if( ScrH() > 600 ) then
		y = 100;
	end

	LetterVGUI = vgui.Create( "Panel" );
	LetterVGUI:SetPos( ScrW() / 2 - 300, y );
	LetterVGUI:SetSize( 600, 600 );
	LetterVGUI.Paint = function()
	
		draw.RoundedBox( 0, 0, 0, LetterVGUI:GetWide(), LetterVGUI:GetTall(), Color( 255, 255, 255, 200 ) );
		draw.DrawText( LetterContent, "LetterFont", 5, 5, Color( 0, 0, 0, 255 ) );
	
	end
	
	LetterVGUI.Menu = CreateBPanel( "Menu", ScrW() / 2 + 300, y, 100, 30 );
	LetterVGUI.Menu:CanSeeTitle( false );
	LetterVGUI.Menu:AddButton( "Close", 5, 5, function() LetterVGUI:SetVisible( false ); LetterVGUI.Menu:SetVisible( false ); HideMouse(); end );
	
	gui.EnableScreenClicker( true );
	
end
usermessage.Hook( "DL", DisplayLetter );

function SendLetterPiece( msg )

	UnfLetterContent = UnfLetterContent .. string.gsub( msg:ReadString(), "@n", "\n" );
	
	LetterContent = FormatLine( UnfLetterContent, "LetterFont", 580 );

end
usermessage.Hook( "SLP", SendLetterPiece );

function CitizenGuide( msg )
	
	if( LetterVGUI ) then
		LetterVGUI:Remove();
	end
	
	local y = 0;

	if( ScrH() > 600 ) then
		y = 100;
	end

	LetterVGUI = vgui.Create( "Panel" );
	LetterVGUI:SetPos( ScrW() / 2 - 300, y );
	LetterVGUI:SetSize( 600, 600 );
	LetterVGUI.Paint = function()
	
		draw.RoundedBox( 0, 0, 0, LetterVGUI:GetWide(), LetterVGUI:GetTall(), Color( 255, 255, 255, 200 ) );
		draw.DrawText( "Citizen's Handbook" , "ChatFont", LetterVGUI:GetWide() / 2 - 95 , 5, Color( 0, 0, 0, 255 ) );
		draw.DrawText( "This book is property of Civil Protection\n Do not destroy or damage in anyway.\n\n Property Ownership:\n Owning an apartment or storefront is a privilage.\n Remember to always keep your property kept in good condition.\n\n Always treat Protection Teams with kindness and follow directions.\n To inquire of a job as a CP, first prove yourself a loyal citizen." , "LetterFont", 10 , 35, Color( 0, 0, 0, 255 ) );
	
	end
	
	LetterVGUI.Menu = CreateBPanel( "Menu", ScrW() / 2 + 300, y, 100, 30 );
	LetterVGUI.Menu:CanSeeTitle( false );
	LetterVGUI.Menu:AddButton( "Close", 5, 5, function() LetterVGUI:SetVisible( false ); LetterVGUI.Menu:SetVisible( false ); HideMouse(); end );
	
	gui.EnableScreenClicker( true );
	
end
usermessage.Hook( "CG", CitizenGuide );

function CPGuide( msg )
	
	if( LetterVGUI ) then
		LetterVGUI:Remove();
	end
	
	local y = 0;

	if( ScrH() > 600 ) then
		y = 100;
	end

	LetterVGUI = vgui.Create( "Panel" );
	LetterVGUI:SetPos( ScrW() / 2 - 300, y );
	LetterVGUI:SetSize( 600, 800 );
	LetterVGUI.Paint = function()
	
		draw.RoundedBox( 0, 0, 0, LetterVGUI:GetWide(), LetterVGUI:GetTall(), Color( 255, 255, 255, 200 ) );
		draw.DrawText( "Civil Protection Handbook" , "ChatFont", LetterVGUI:GetWide() / 2 - 125 , 5, Color( 0, 0, 100, 255 ) );
		draw.DrawText( "This book is property of Civil Protection\n Do not destroy or damage in any way.\n\n Classifications\n\n Anti-Citizen: Criminals\n Malignant: Highly dangerous and contagious.\n Infected: Unfriendly ( Refers to Humanoid. )\n Biotic: Antlion or Myrmidont.\n Infestation: High parasite activity\n Parasite: Headcrab\n Necrotic: Zombie\n Outland: Foreign\n Virome: Vortigaunt\n\n Status\n Deserviced: Unit down, Officer unable to perform duties.\n In Position: Ready to perform orders.\n Standing by: Awaiting orders.\n Outbreak: Last remaining unit in group.\n Holding: Maintainging and guarding position.\n Malcompliant: Unable to carry out Civil Obligations.\n\n Actions\n Clamp: Restrict passage, block all Civil Passage through area.\n Anti-Civil: Anything that negatively effects the citizen population.\n Breach: Remove obstacle or defences. \n Stabilize: Restore order through any means required verbally or otherwise.\n Amputate: Execute, destroy.\n Sterilize: Free area or situation of.\n Isolate: Seclude a citizenor a group of citizens from the rest of the population.\n Jurisdiction: Choice of reasoning.\n Stabilization: Stimulation of the Citizen Population during a time of unrest.\n Contain: Prevent forces or situation from spreading beyond its current location.\n Extract: Evacuate\n Pacify: Detain or Neutralize through any means necessary. ( Nonlethal if possible.)\n" , "LetterFont", 10 , 35, Color( 0, 0, 0, 255 ) );
	
	end
	
	LetterVGUI.Menu = CreateBPanel( "Menu", ScrW() / 2 + 300, y, 100, 30 );
	LetterVGUI.Menu:CanSeeTitle( false );
	LetterVGUI.Menu:AddButton( "Close", 5, 5, function() LetterVGUI:SetVisible( false ); LetterVGUI.Menu:SetVisible( false ); HideMouse(); end );
	
	gui.EnableScreenClicker( true );
	
end
usermessage.Hook( "CPG", CPGuide );


ActionMenuOn = false;
ActionMenuTitle = "";
ActionMenuOptions = { }
ActionMenuPos = Vector( 0, 0, 0 );
ActionMenuLongest = 0;
ActionMenuChoice = 0;

function ActionMenu( msg )

	ActionMenuOn = true;
	ActionMenuLongest = 0;

	ActionMenuFade = 1;

	ActionMenuTitle = msg:ReadString();
	ActionMenuPos = msg:ReadVector();
	local n = msg:ReadShort();
	local options = { }
	
	for j = 1, n do
	
		table.insert( options, { Name = msg:ReadString(), Command = msg:ReadString(), ID = msg:ReadShort() } );
		
		surface.SetFont( "NewChatFont" );
		local w = surface.GetTextSize( options[j].Name );
		
		if( w > ActionMenuLongest ) then
			ActionMenuLongest = w;
		end
	
	end
	
	ActionMenuLongest = math.Clamp( ActionMenuLongest, 100, 9999 );
	
	if( ActionMenuLongest > 100 ) then
		ActionMenuLongest = ActionMenuLongest + 6;
	end
	
	ActionMenuOptions = options;
	
end
usermessage.Hook( "AM", ActionMenu );

ProcessBars = { }

function CreateCustomProcessBar( msg )

	local bar = { }
	
	local id = msg:ReadString();

	bar.Title = msg:ReadString();
	bar.Time = msg:ReadFloat();
	bar.StartTime = CurTime();
	bar.EndTime = CurTime() + bar.Time;
	bar.Color = Color( msg:ReadShort(), msg:ReadShort(), msg:ReadShort(), msg:ReadShort() );
	
	ProcessBars[id] = bar;


end
usermessage.Hook( "CCPB", CreateCustomProcessBar );

function CreateProcessBar( msg )

	local bar = { }
	
	local id = msg:ReadString();

	bar.Title = msg:ReadString();
	bar.Time = msg:ReadFloat();
	bar.StartTime = CurTime();
	bar.EndTime = CurTime() + bar.Time;
	bar.Color = Color( 70, 70, 200, 90 );
	
	ProcessBars[id] = bar;

end
usermessage.Hook( "CPB", CreateProcessBar );

function RemoveProcessBar( msg )

	local id = msg:ReadString();
	
	ProcessBars[id] = nil;

end
usermessage.Hook( "RPB", RemoveProcessBar );

function RemoveAllProcessBars( msg )

	for k, v in pairs( ProcessBars ) do
	
		ProcessBars[k] = nil;
	
	end

end
usermessage.Hook( "RAPB", RemoveAllProcessBars );

FadeStart = 0;
FadeEnd = 0;
FIFOAlpha = 0;
FIFOWait = 0;

--Use only for intro
function FadeInFadeOut( msg )

	FadeStart = CurTime();
	FadeWait = msg:ReadFloat();
	FadeEnd = FadeWait / 2 + CurTime();
	FIFOAlpha = 0;

end
usermessage.Hook( "FIFO", FadeInFadeOut );

function TurnOffOWSight()

	OWSight = false;

end
usermessage.Hook( "TOFFOWS", TurnOffOWSight );

function ToggleOWSight()

	OWSight = !OWSight;

end
usermessage.Hook( "TOWS", ToggleOWSight );

AdminSeeAll = false;

function SeeAll()

	AdminSeeAll = !AdminSeeAll;

end
usermessage.Hook( "SeeAll", SeeAll );

function UpdateDoor( msg )

	local ent = msg:ReadEntity();
	local property = msg:ReadString();
	local name = msg:ReadString();
	
	ent:GetTable().PropertyName = property;
	ent:GetTable().DoorName = name;

end
usermessage.Hook( "UDD", UpdateDoor );

function UpdatePlayerTitle( msg )

	local ply = msg:ReadEntity();
	local title = msg:ReadString();

	ply:GetTable().PlayerTitle = title;

end
usermessage.Hook( "UDPT", UpdatePlayerTitle );

function UpdatePlayerSecondTitle( msg )

	local ply = msg:ReadEntity();
	local title2 = msg:ReadString();

	ply:GetTable().PlayerSecondTitle = title2;

end
usermessage.Hook( "UDPST", UpdatePlayerSecondTitle );

StunStickSlapTime = 0;
StunStickWhiteAlpha = 0;
StunStickBlurMul = 0;

function DoStunStickSlap()

	StunStickSlapTime = CurTime();
	StunStickWhiteAlpha = 255;
	StunStickBlurMul = 1;

end
usermessage.Hook( "DSSS", DoStunStickSlap );

function HolsterPos()

	local weap = LocalPlayer():GetActiveWeapon();
	
	if( weap:IsValid() and weap:GetTable() and weap:GetTable().Primary ) then

		weap:GetTable().Primary.PositionMode = 3;
		weap:GetTable().Primary.PositionMul = 1;
		
	end
	

end
usermessage.Hook( "HPOS", HolsterPos );

--Too lazy to convert old shit to events.  Using events from now on.
function event.MapViewOn()

	TS.MapView = true;

end

function event.MapViewOff()

	TS.MapView = false;

end

WaterBlur =
{

	LastUpdate = 0,
	Add = 0,
	Draw = 0,
	Delay = 0,
	MaxAdd = .07,
	MaxDraw = .39,
	MaxDelay = 0.03,

}

--water bottle effect
function event.WaterBlur()

	WaterBlur.Add = math.Clamp( WaterBlur.Add + 0.014, 0, WaterBlur.MaxAdd );
	WaterBlur.Draw = math.Clamp( WaterBlur.Draw + .078, 0, WaterBlur.MaxDraw );
	WaterBlur.Delay = math.Clamp( WaterBlur.Delay + .006, 0, WaterBlur.MaxDelay );
	WaterBlur.LastUpdate = CurTime();

end

AlcoholBlur =
{

	LastUpdate = 0,
	Add = 0,
	Draw = 0,
	Delay = 0,
	HeadBob = 0,
	HeadBobPitchMul = 1,
	HeadBobYawMul = 1,
	HeadBobCosine = 0,
	MaxHeadBob = 1,
	MaxAdd = .21,
	MaxDraw = 1,
	MaxDelay = 0.09,
	ShiftedPitchYaw = false,

}


function event.HAlcoholBlur()

	AlcoholBlur.Add = math.Clamp( AlcoholBlur.Add + AlcoholBlur.MaxAdd * .14, 0, AlcoholBlur.MaxAdd );
	AlcoholBlur.Draw = math.Clamp( AlcoholBlur.Draw + AlcoholBlur.MaxDraw * .14, 0, AlcoholBlur.MaxDraw );
	AlcoholBlur.Delay = math.Clamp( AlcoholBlur.Delay + AlcoholBlur.MaxDelay * .14, 0, AlcoholBlur.MaxDelay );
	AlcoholBlur.LastUpdate = CurTime();

end

function event.HangOver()

	AlcoholBlur.Add = math.Clamp( AlcoholBlur.MaxAdd * .5, 0, AlcoholBlur.MaxAdd );
	AlcoholBlur.Draw = math.Clamp( AlcoholBlur.MaxDraw * .5, 0, AlcoholBlur.MaxDraw );
	AlcoholBlur.Delay = math.Clamp( AlcoholBlur.MaxDelay * .5, 0, AlcoholBlur.MaxDelay );
		

end

function event.NoDrunk()

	AlcoholBlur.Add = 0;
	AlcoholBlur.Draw = 0;
	AlcoholBlur.Delay = 0;

end

---------------------------------------
-- MOTD CREATION 
---------------------------------------

MOTDText = "";

if( MOTD ) then
	
	if( MOTD.TitleBar ) then

		MOTD.TitleBar:Remove();
		MOTD.TitleBar = nil;
		
	end
		
	MOTD:Remove();
	MOTD = nil;
end
function CreateMOTD()

	if( MOTD ) then
		MOTD:Remove();
		MOTD = nil;
	end

	MOTD = CreateBPanel( "Read This", ScrW() / 2 - 250, math.Clamp( ScrH() * .1 + 40, 180, 9999 ), 500, 400 );
	MOTD:CanClose( false );
	MOTD:CanDrag( false );

	FillMOTD();

	gui.EnableScreenClicker( true );

end
usermessage.Hook( "MOTD", CreateMOTD );

function FillMOTD()

	if( MOTD.Text ) then
		MOTD.Text:Remove();
	end

	MOTD.Text = MOTD:AddLabel( MOTDText, "NewChatFont", 5, 3, Color( 255, 255, 255, 255 ) );

	MOTD:EnableScrolling( true );
	
	local function CloseMOTD()
	
		RunConsoleCommand( "rp_closemotd" );
		
		timer.Simple( 2, function()
			TS.FancyGayIntro = false;
			TS.MapView = false;
		end );
		
		MOTD.TitleBar:Remove();
		MOTD:Remove();
	
		HideMouse();
		
		LocalPlayer():GetTable().OutOfMenu = true
	
	end
	
	if( MOTD.ExitButton ) then
		MOTD.ExitButton:Remove();
	end
	
	MOTD.ExitButton = MOTD:AddButton( "Done", 225, MOTD.Text:GetTall() + 50, CloseMOTD );


end

function msgLoadCharacterDetails()

	timer.Simple( 2, function()
			TS.FancyGayIntro = false;
			TS.MapView = false;
		end );

end
usermessage.Hook( "LCD", msgLoadCharacterDetails )	

function RestartMOTD()

	MOTDText = "";

end
usermessage.Hook( "RMOTD", RestartMOTD );

function SendMOTDPiece( msg )
	
	MOTDText = MOTDText .. msg:ReadString();
	
	MOTDText = FormatLine( MOTDText, "NewChatFont", 480 );
	
	if( MOTD and MOTD:IsVisible() ) then
		FillMOTD();
	end

end
usermessage.Hook( "SMP", SendMOTDPiece );

function RSD()

	ChosenCharacterModel = nil
	
end
usermessage.Hook( "RCD", RSD )	

